﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 软件运行平台.View
{
    /// <summary>
    /// LogInView.xaml 的交互逻辑
    /// </summary>
    public partial class LogInView : Window
    {
        public bool? logIn = null;
        string _url = "";
        /// <summary>
        /// 登录界面
        /// </summary>
        /// <param name="model">人员信息</param>
        /// <param name="ids">历史账号</param>
        public LogInView(string url)
        {
            InitializeComponent();
            _url = url;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //登录
            if (ComboBoxId.Text == "")
            {
                HandyControl.Controls.Growl.Warning("请输入账号！");
                return;
            }
            // 使用.NET内部算法把IntPtr指向处的字符集合转换成字符串 
            IntPtr p = Marshal.SecureStringToBSTR(this.PasswordBoxPass.SecurePassword);
            // 顺便校验一下
            string password = Marshal.PtrToStringBSTR(p);
            if (password == "")
            {
                HandyControl.Controls.Growl.Warning("请输入密码！");
                return;
            }
            try
            {
                string id = ComboBoxId.Text.ToLower();
                string err = "";
                MesTool.Model.UserModel model = null;
                if (_url.Substring(_url.Length - 1, 1).Equals("="))
                {
                    model = MesTool.UserBLL.GetUserName(id, ref err, _url);
                    if (model == null)
                    {
                        HandyControl.Controls.Growl.Warning(err);
                        return;
                    }
                }
                else
                {
                    model = MesTool.UserBLL.Login(id, password, ref err);
                    if (model == null)
                    {
                        HandyControl.Controls.Growl.Warning(err);
                        return;
                    }
                }
                MengXunAppTool.Helper.LogHelper.WriteLog(model.nickName + "登录");
                logIn = true;
                MainWindow.userModel = model;
                try
                {
                    this.DialogResult = true;
                }
                catch
                {
                }
                this.Close();


            }
            catch (Exception ex)
            {
                MengXunAppTool.Helper.LogHelper.WriteLog(ex.ToString());
                HandyControl.Controls.Growl.Error(ex.Message);
                return;
            }
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            //回车键
            if (e.Key == Key.Enter)
            {
                Button_Click(null, null);
            }
            //Esc键
            else if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBoxId.Focus();
            //动画
            DoubleAnimation opacityAnimation = new DoubleAnimation();
            opacityAnimation.From = 0.1;
            opacityAnimation.To = 1;
            opacityAnimation.Duration = TimeSpan.FromSeconds(0.5);
            StackPanelInfo.BeginAnimation(StackPanel.OpacityProperty, opacityAnimation);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (logIn == null)
            {
                logIn = false;
            }
        }
    }
}
